package com.ling.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import pojo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("t1")
    public String test1(@RequestParam("username") String name, Model model){
        //1.接受前端参数 http://localhost:8080/user/t1?username=11
        System.out.println("======="+name);
//        2.将返回的结果传递给前端,Model
        model.addAttribute("msg",name);
//        3.视图跳转
        return "test";
    }

//http://localhost:8080/user/t3?name=王&id=1&age=24
    @GetMapping("t2")
    public String test2(User user){
        System.out.println("前端传递来的参数为:"+user);
        return "test";
    }

    @GetMapping("t3")
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse response){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","handleRequest");
        mv.setViewName("test");
        return mv;
    }

    @GetMapping("/t4")
    public String test4(@RequestParam("username") String name, ModelMap model){
        //封装要显示到视图中的数据 http://localhost:8080/user/t4?username=adwa3
        //相当于req.setAttribute("name",name);
        model.addAttribute("msg",name);

        return "test";
    }


}
